// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameConfig, { ShapeType } from "../GameConfig";
import GameData from "../GameData";
import ShapeManager from "../shape/ShapeManager";
import UINode, { UIType } from "../ui/UINode";
import Utlis from "../Utlis";



const {ccclass, property} = cc._decorator;

@ccclass
export default class UIGame extends UINode {
    private preview1:cc.Button = null;
    private preview2:cc.Button = null;
    private preview3:cc.Button = null;
    public uiType(): UIType {
        return UIType.Normal;
    }

    protected async start() {
        this.findComponent();
        this.preview1.node.on(cc.Node.EventType.TOUCH_END, this.onClickPreview1, this);
        this.preview2.node.on(cc.Node.EventType.TOUCH_END, this.onClickPreview2, this);
        this.preview3.node.on(cc.Node.EventType.TOUCH_END, this.onClickPreview3, this);


        GameData.randomShapeBatcher();

        for (let i = 0; i < GameData.currentWaitSelectShapeBatcher.length; i++) {
            const shapeData = GameData.getWaitSelectShapeData(i);
            if (shapeData) {
                const shape = await Utlis.createShape(shapeData.type, shapeData.color);
                if (this["preview"+(i+1)]) {
                    this["preview"+(i+1)].node.addChild(shape);
                }
            }
        }
    }

    private findComponent() {
        this.preview1 = cc.find("previewLayout/preview1", this.node).getComponent(cc.Button);
        this.preview2 = cc.find("previewLayout/preview2", this.node).getComponent(cc.Button);
        this.preview3 = cc.find("previewLayout/preview3", this.node).getComponent(cc.Button);
    }

    private onClickPreview1() {
        console.warn("onClickPreview1");
        const shapeData = GameData.getWaitSelectShapeData(0);
        ShapeManager.getInstance().createShape(shapeData);
    }
    private onClickPreview2() {
        console.warn("onClickPreview2");
        const shapeData = GameData.getWaitSelectShapeData(1);
        ShapeManager.getInstance().createShape(shapeData);
    }
    private onClickPreview3() {
        console.warn("onClickPreview3");
        const shapeData = GameData.getWaitSelectShapeData(2);
        ShapeManager.getInstance().createShape(shapeData);
    }
}
